
import abspkg from '../../graph/action/absaction.js'
import {LGXEnums} from '../../common/sysenum.js'

class ImageRecthAction extends abspkg.AbsGraphAction {
    constructor(graph) {
        super(graph);
    }

    act2Mouse(mosEvnt, worldPos) {
        var graph = this.getRefGraph();
        if (graph == null) {
            return LGXEnums.LGXPlotState.ErrorState;
        }
        var state = LGXEnums.LGXPlotState.NoneState;
        var position = graph.getPos();
        if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDown) {
            if (graph.getStart() == 0) {
                position.left = worldPos.x;
                position.top = worldPos.y;
                position.right = worldPos.x;
                position.bottom = worldPos.y;
                graph.setStart(1);
                state = LGXEnums.LGXPlotState.AppendPtState;
            }
        }
        else if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseUp
            && graph.getFinish() == 0) {
            this.correctRectPos(position,worldPos);
            // graph.setFinish(1);
            state = LGXEnums.LGXPlotState.FinistState;
        }
        else if ((mosEvnt.type == LGXEnums.LGXMouseMode.mouseMove
            || mosEvnt.type == LGXEnums.LGXMouseMode.mouseDrag)
            && graph.getFinish() == 0) {
            this.correctRectPos(position,worldPos);
            state = LGXEnums.LGXPlotState.OKState;
        }
        graph.calScope(1);
        return state;
    }

    correctRectPos(position,mousePos)
    {
        position.left = Math.min(position.left,mousePos.x);
        position.right = Math.max(position.right,mousePos.x);
        position.bottom = Math.min(position.bottom,mousePos.y);
        position.top = Math.max(position.top,mousePos.y);
    }
}

export default { ImageRecthAction }
